﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using YDModel;
using YDServices.Comm;

namespace YDServices
{
    public class SystemParameterService : BaseService
    {
        
        /// <summary>
        /// 通用下拉框
        /// </summary>
        /// <param name="Parameter_Code"></param>
        /// <returns></returns>
        public List<T_SystemParameter_List> getComboxList(string Parameter_Code)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                   var list = db.T_System_Parameter.Where(o => o.Type_Code == Parameter_Code && o.State==true) .ToList() ;
                    List<T_SystemParameter_List> spList = new List<T_SystemParameter_List>();
                    if (list.Count>0)
                    { 
                        foreach (var item in list)
                        {
                            T_SystemParameter_List sp = new T_SystemParameter_List();
                            sp.Value =string.IsNullOrEmpty( item.Parameter_Value)?0: int.Parse(item.Parameter_Value);
                            sp.Name = item.Parameter_Name;
                            spList.Add(sp);
                        }
                    }


                    return spList;
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }


        /// <summary>
        /// 获取分类
        /// </summary>
        public List<T_Parameter_Type_List> getTypeList()
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var list = db.T_System_Parameter.Where(o => o.Type_Name != null)
                        .Select(o=>new T_Parameter_Type_List { Type_Code=o.Type_Code,Type_Name= o.Type_Name }).Distinct().ToList(); 

                    return list;
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }



        /// <summary>
        /// 通用下拉框
        /// </summary>
        /// <param name="Parameter_Code"></param>
        /// <returns></returns>
        public List<T_System_Parameter> getList(string Type_Code)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var list = db.T_System_Parameter.Where(o => o.Type_Code == Type_Code && o.State==true).ToList(); 

                    return list;
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="tu"></param>
        public void AddItem(T_System_Parameter tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var user = db.T_System_Parameter.Where(o => o.Parameter_Name == tu.Parameter_Name
                        && o.State == true && o.Type_Code==tu.Type_Code).FirstOrDefault();
                    if (user != null)
                    {
                        throw new ApplicationException("参数名称已存在!");
                    }

                    tu.State = true;
                    tu.CreatedTime = GetSqlNow();
                    tu.CreatedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                    db.T_System_Parameter.Add(tu);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="tu"></param>
        public void EditItem(T_System_Parameter tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var item = db.T_System_Parameter.Where(o => o.ID == tu.ID && o.State == true).FirstOrDefault();
                    if (item == null)
                    {
                        throw new ApplicationException("参数信息不存在!");
                    }
            
                    item.Parameter_Name = tu.Parameter_Name; 
                    item.ModifiedTime = GetSqlNow();
                    item.ModifiedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="tu"></param>
        /// <param name="loginUser"></param>
        public void DelItem(T_System_Parameter item)   
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {

                    var fact = db.T_System_Parameter.Where(o => o.ID == item.ID).FirstOrDefault();
                    if (fact == null)
                    {
                        throw new ApplicationException("参数信息不存在!");
                    }
                    fact.State = false;
                    fact.DeletedTime = GetSqlNow();
                    fact.DeletedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;

                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



    }
}
